var k = 0;
var orClick = true;
var imgSrc = "";
//点击上传图片
function getToken(){
    var uploadFile = document.getElementById("hide");
    ajax({
        url:pathName+"/api/v1/member/qiniuUploadToken",
        method:"POST",
        dataType:"json",
        success:function(data){
            console.log(data);
            var obj = JSON.parse(data);
            uploadFile.value = obj.success;
        }
    })
}
function info(message,status){
    var ts = document.querySelector('.TS');
    switch (status){
        case 0:
            ts.style = '';
            break;
        case 1:
            ts.style.backgroundColor = 'rgba(19,198,191,.8)';
            break;
        default :
            break;
    }
    ts.innerHTML = message;
    ts.style.opacity = 1;
    setTimeout(function () {
        ts.style.opacity = 0;
    }, 1000)
}
//更换图形验证码
function changeImg(){
    var imgCon = document.querySelector("#imgCon");
    var input = document.getElementById('phonenumber');
    Tap(imgCon,function(){
        $.ajax({
            url:pathName+"/api/v1/member/createVerifyCodeImg",
            type:"POST",
            data:{
                phone:input.value
            },
            success:function(res){
                img.src = res.success.verifyCodeImg;
            }
        })
    })
}
//提交图片END
function numbreg() {
    var em = document.querySelector('form').querySelector('em');
    var ts = document.querySelector('.TS');
    var input = document.getElementById('phonenumber');
    var yzm = document.getElementById('yzm');
    var pass = document.getElementById('pass');
    var zhuce = document.querySelector('.denglu');

    var reg = /^1[3-9][0-9]{9}$/;
    var regpass = /^\w{6,16}$/;
    function errPhonenumber(){
        k = 0;
        info("请输入正确的手机号",0);
    }
    input.onblur = function () {
        if (!reg.test(this.value)) {
            errPhonenumber();
        } else {
            k = 1;
        }
    };
    pass.onblur = function () {
        if (!regpass.test(this.value)) {
            k = 0;
            info("请输入6-16位密码",0);
        } else {
            k = 1;
        }
    };
    //获取验证码
    var timer;
    Tap(em, function (e,dom) {
        var phrasenode = document.querySelector("#imgyzm");
        var phrase = phrasenode.value || "";
        console.log(phrase)
        if(orClick){
            if (!reg.test(input.value)) {
                errPhonenumber();
            } else {
                orClick = false;
                dom.innerHTML = 60;
                var preNum = 60;
                dom.style.background = 'lightgray';
                timer = setInterval(function(){
                    preNum--;
                    dom.innerHTML = preNum;
                    if(preNum<=0){
                        orClick = true;
                        dom.style.background = '';
                        dom.innerHTML = "重新发送";
                        clearInterval(timer);
                    }
                },1000);
                $.ajax({
                    url:pathName+"/api/v1/member/sendSms",
                    type:"POST",
                    dataType:"json",
                    header:{
                        "Content-Type":"application/x-www-form-urlencoded;charset=UTF-8;application/json"
                    },
                    data:{
                        "phone":input.value,
                        "phrase":phrase
                    },
                    success:function(obj) {
                        //var obj = JSON.parse(data);
                        console.log(obj);
                        if (obj.status == 406) {
                            orClick = true;
                            ts.style.backgroundColor = '';
                            ts.innerHTML = obj.error;
                            ts.style.opacity = 1;
                            setTimeout(function () {
                                ts.style.opacity = 0;
                            }, 1000);
                            em.style.background = '';
                            em.innerHTML = "获取验证码";
                            clearInterval(timer);
                        } else {
                            info("已经发送验证码",1);
                        }
                    },
                    error:function(data){
                        console.log(data);
                        var obj = JSON.parse(data.responseText);
                        orClick = true;
                        if(obj.code == 20008){
                            clearInterval(timer);
                            dom.innerHTML = "获取验证码";
                            dom.style.background = "";
                            info("该用户已注册",0);
                        }else if(obj.code == 20010){
                            clearInterval(timer);
                            dom.innerHTML = "获取验证码";
                            dom.style.background = "";
                            info("验证码发送失败",0);
                        }else if(obj.code == 20040){
                            dom.innerHTML = "获取验证码";
                            info("图形验证码有误",0);
                            dom.style.background = "";
                            clearInterval(timer);
                            $.ajax({
                                url:pathName+"/api/v1/member/createVerifyCodeImg",
                                type:"POST",
                                data:{
                                    phone:input.value
                                },
                                success:function(res){
                                    var main = document.getElementById("main");
                                    var photoyzm = document.querySelector(".map");
                                    var imgyzm = document.getElementById("imgyzm");
                                    var imgCon = document.getElementById("imgCon");
                                    var img = imgCon.querySelector("img");
                                    main.setAttribute("class","styletwo");
                                    imgyzm.style.display = "block";
                                    imgCon.style.display = "block";
                                    photoyzm.style.display = "block";
                                    img.src = res.success.verifyCodeImg;
                                    changeImg();
                                }
                            })
                        }
                    }
                })
            }
        }
    });
    //上传图片部分
    $("#uploadFile").on("change",function() {
        //改变输入框文字
        var file0 = document.querySelector('.file');
        var filed = document.querySelector('.filed');
        var file_head = filed.querySelector(".file_head");
        var file_type = filed.querySelector(".file_type");
        var k = file0.value;
        k = k.split("\\");
        k = k[k.length - 1];
        file_head.style.width = "";
        file_head.innerHTML = k;
        //如果图片名字过长，隐藏部分名字，保留格式
        var width = file_head.clientWidth;
        var parentWidth = filed.clientWidth;
        if(width > parentWidth-100){
            file_head.innerHTML = k.split(".")[0];
            file_head.style.width = "calc(100% - 116px)";
            file_type.innerHTML = "." + k.split(".")[1];
            console.log(k.split(".")[1]);
        }else{
            file_type.innerHTML = "";
        }
        //改变右边图标
        var s = document.querySelector("#main form s");
        s.innerHTML = "替换";
        s.style.cssText = 'background:rgba(0,0,0,0);border:1px solid black;color:black;padding:0 9px;font-size:12px';
        var mask = document.querySelector(".mask");
        //普通上传
        mask.style.display = "block";
        var Qiniu_UploadUrl = "http://up.qiniu.com";
        var Qiniu_upload = function(f, token, key) {
            var xhr = new XMLHttpRequest();
            xhr.open('POST', Qiniu_UploadUrl, true);
            var formData;
            formData = new FormData();
            if (key !== null && key !== undefined) formData.append('key', key);
            formData.append('token', token);
            formData.append('file', f);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText != "") {
                    var blkRet = JSON.parse(xhr.responseText);
                    console && console.log(blkRet);
                    console.log(blkRet.hash);
                    imgSrc = blkRet.hash;
                    //var imgsa = document.querySelector("#imgsa");
                    //imgsa.src = "http://img.like-your.com/"+imgSrc;
                    imgSrc = "http://img.like-your.com/"+imgSrc+ "?imageslim";
                    mask.style.display = "none";
                } else if (xhr.status != 200 && xhr.responseText) {
                    mask.style.display = "none";
                    info("图片上传失败，请重新上传",0);
                }
            };
            xhr.send(formData);
        };
        var token = $("#hide").val();
        if ($("#uploadFile")[0].files.length > 0 && token != "") {
            Qiniu_upload($("#uploadFile")[0].files[0], token, $("#key").val());
        } else {
            console && console.log("form input error");
        }
    })
    //上传图片END
    //点击注册
    Tap(zhuce, function () {
        var companyName = document.getElementsByClassName("last")[0];
        var address = document.getElementById("adress");
        var yzm = document.querySelector("#yzm");
        var leader = document.getElementById("leader");
        if(input.value && yzm.value && pass.value && companyName.value && address.value){
            if (reg.test(input.value) && regpass.test(pass.value)){
                ts.style.backgroundColor = 'rgba(19,198,191,.8)';
                ts.innerHTML = '注册中...';
                ts.style.opacity = 1;
                //return true;
                var mask = document.querySelector(".mask");
                mask.style.display = "block";
                $.ajax({
                    url:pathName+"/api/v1/member/register",
                    type:"POST",
                    dataType:"json",
                    headers:{
                        // "Content-Type":"application/json"
                    },
                    data:{
                        "code":yzm.value,
                        "phone":input.value,
                        "password":pass.value,
                        "companyName":companyName.value,
                        "companyAddress":address.value,
                        "companyPhoto":imgSrc,
                        "partnerPhone":leader.value
                    },
                    success:function(obj){
                        console.log(obj);
                        //var obj = JSON.parse(data.responseText);
                        mask.style.display = "none";
                        ts.style.backgroundColor = 'rgba(19,198,191,.8)';
                        ts.innerHTML = '注册已提交，等待后台审核...';
                        ts.style.opacity = 1;
                        setTimeout(function () {
                            ts.style.opacity = 0;
                            window.location.href = "../../index.html";
                        }, 1500);
                    },
                    error:function(data){
                        console.log(data);
                        mask.style.display = "none";
                        var obj = JSON.parse(data.responseText);
                        info(obj.message,0)
//                      if(obj.code == 20004){
//                          info("用户已存在，请直接登陆",0);
//                      }else if(obj.code ==20003){
//                          info("手机验证码有误，请重新输入",0);
//                      }else if(obj.code == 20005){
//                          info("注册失败",0);
//                      }else if(obj.code == 20030){
//                          info("推荐码错误",0)
//                      }else if(obj.code == 20043){
//                          info("该城市合伙人不存在",0)
//                      }
                    }
                })
            }else{
                info("网络连接出错，请重试",0);
            }
            return false;
        }else{
            info("请完善您的信息",0);
        }


    });
}
//正则匹配END
window.onload = function () {
    numbreg();
};